<?php
include("functions.php");
include("libs/smarty.class.php");

$smarty = new Smarty();
$title = "Szczegóły praktyki.";
$smarty->assign("title",$title);

$practices = array();
$practice_id = $_GET['id'];

print_r($_POST);

if($practice_id!=''){
    $practiceDetails = getPracticeDetails($practice_id);
    $smarty->assign("p",$practiceDetails['practice']);
    $smarty->assign("c",$practiceDetails['company']);
    $smarty->assign("pe",$practiceDetails['person']);
    $smarty->assign("practiceDetails",$practiceDetails);
}

if($_POST['send']){
    //CHECK LOGGED
    $practice_id = $_POST['practice_id'];
    if(isset($_SESSION['user_id'])){
        $student_id = $_SESSION['user_id'];
        //CHECK STUDENT
        if($_SESSION['user_type'] == 'user_student'){
            //CHECK PRACTICE AMOUNT
            $amount = getRow("amount", $practice_id, "practices");
            $amount = $amount['0']['amount'];
            echo $amount;
            if($amount > 0 ){
                //CHECK STUDENT PRACTICE
                $practice = getRow("practice_id", $student_id,"user_student");

                //CHECK PRACTICE QUEUE
                $check = mysql_query("SELECT spq.id FROM student_practices_queue spq WHERE spq.student_id ='$student_id'");
                if(mysql_num_rows($check) == 0){
                    //ADD TO QUEUE
                    $student_practice_id = $practice['0']['practice_id'];
                    if($student_practice_id == 0){
                        $query = mysql_query("INSERT INTO `student_practices_queue` (`practice_id`,`student_id`)
                            VALUES ('$practice_id','$student_id')");
                        $amount-=1;
                        $query2 = mysql_query("UPDATE `practices` SET amount = '$amount'  WHERE id = '$practice_id'");
                        $info = getInfo(6);
                    }else{
                        $info = getInfo(5);
                    }
                }else{
                   $info = getInfo(4);
                }
            }else{
                $info = getInfo(3);
            }
        }else{
            $info = getInfo(2);
        }
    }else{
         $info = getInfo(1);
    }
}
$smarty->assign("info",$info);
$smarty->display('practice_details.tpl');
?>
